<?php
## include required files
/*******************************/
require_once '../../includefiles.php';
require_once '../../model/users.php';
require_once '../../model/admindetails.php';
require_once '../../model/email.php';

if($_SESSION['getbids_id']!='')
{
	header("Location: ".SITE_URL."/");
	exit;		
}


## Create Objects
/*******************************/
$userObj = new Model_Users();
$adminObj = new Model_AdminDetails();
$emailObj = new Model_Email();
/*******************************/

if(isset($_POST['forgot']))
{   
	$username     = trim($_POST['username']);
	$result   = $userObj->chkUserNameExist($username);
	//echo"<pre>"; print_r($result); exit;
	$cnt      = count($result);
  
    if($cnt > 0)
	{
		if($result['user_status']!='0')
		{
			$link		= SITE_URL."/signin/".md5($result['id'])."/resetpassword/";		 
			$link		= '<a href="'.$link.'">'.$link.'</a>';		 
			
			$username 	= $result['first_name'];
			$emailArray	= $emailObj->getEmailById(2);
			$to			= $result['email'];
			$subject 	= $emailArray['email_subject'];
			$adminEmail	= $adminObj->getAdminDetails();
			$message	= $emailArray['email_content'];
			
			$message	= str_replace('[NAME]', $username, $message);
			$message	= str_replace('[ACTIVATIONURL]', $link, $message);
			$message	= str_replace('[SITENAME]',SITENAME , $message);
			$message	= str_replace('[SITE_LINK]',SITE_URL , $message);
			
			$from		= $adminEmail;
			$fromname 	= SITENAME;
			$emailTemplate = file_get_contents('../../emailtemplate.html');
			
			$template_msg = str_replace('[SITENAME]',SITENAME , $emailTemplate);
			$template_msg = str_replace('[LOGO]','<img src="'.SITE_URL.'/uploads/logo/'.$imageDetails.'">', $template_msg);
			$template_msg = str_replace('[MESSAGE]',$message , $template_msg);
	
			//echo $to; echo $template_msg; echo $subject; echo $from; echo $fromname; exit;
			if(trim($to) != '')
					@send_mail($to, $template_msg, $subject, $from, $fromname);	
			$_SESSION['msg'] = "<div class='success_msg'><span>Reset password link is sent successfully!</span></div>";
		}
		else{
			 $_SESSION['error'] = 'Your account is inactive!';
		}
	}
	else{
	     $_SESSION['error'] = 'Please enter your Email Address which was used for registration!';
	}
}

if($_SESSION['msg']!=''){
	$smarty->assign("successMsg", $_SESSION['msg']);
	unset($_SESSION['msg']);
}

if($_SESSION['error'] != ''){	
	$smarty->assign("errorMsg", $_SESSION['error']);	
	unset($_SESSION['error']);
}

//Active Class
$active = 'login';
$smarty->assign('active', $active);


$smarty->display(TEMPLATEDIR . '/modules/signin/forgotpasswod.tpl');
?>